package com.fun.feifun.database;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.fun.feifun.base.result.Result;
import com.fun.feifun.base.utils.CodeGenerationUtils;
import com.fun.feifun.base.utils.ControlUtils;
import com.fun.feifun.base.utils.ZipUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipOutputStream;

/**
 * <p>
 * 博客_文章 前端控制器
 * </p>
 *
 * @author wkf
 * @since 2020-01-07
 */
@RestController
@Api(value = "/dataBase", tags = "数据库-操作")
@Slf4j
@RequestMapping("/dataBase")
public class DatabaseController {

	/**
	 * 获取数据库中的全部表
	 */
	@GetMapping("/getTable")
	@ApiOperation(value = "获取数据库表")
	public Result getTable(@ApiParam(value = "数据库链接") @RequestParam(value ="url") String url,
			@ApiParam(value = "用户名") @RequestParam(value ="username") String username,
			@ApiParam(value = "密码") @RequestParam(value ="password") String password,
			@ApiParam(value = "驱动类型") @RequestParam(value ="driver") Integer driver){
		Connection connection = null;
		List<String> tableList= new ArrayList<>();
		try {
			connection = JdbcUtils.getConnection(url, username, password);
			tableList= JdbcUtils.getTable(connection);
		} catch (SQLException e) {
			e.printStackTrace();
			return Result.err();
		}finally {
			JdbcUtils.release(connection,null,null);
		}
		return Result.success(tableList);
	}

	@GetMapping("/generatorCode")
	@ApiOperation(value = "生成代码")
	public Result generatorCode(@ApiParam(value = "数据库链接") @RequestParam(value ="url") String url,
			@ApiParam(value = "用户名") @RequestParam(value ="username") String username,
			@ApiParam(value = "密码") @RequestParam(value ="password") String password,
			@ApiParam(value = "表名称") @RequestParam(value ="tableNames") String tableNames,
			@ApiParam(value = "包路径") @RequestParam(value ="packages") String packages,
			@ApiParam(value = "模块名称") @RequestParam(value ="module") String module){

		AutoGenerator mpg =new AutoGenerator();
		//配置生成的 包路径
		long stamp = System.currentTimeMillis();

		GlobalConfig gc = CodeGenerationUtils.globalConfig(stamp);
		mpg.setGlobalConfig(gc);
		//配置模块名
		PackageConfig pc = CodeGenerationUtils.packageConfig(packages,module);
		mpg.setPackageInfo(pc);
		//配置数据源
		DataSourceConfig dsc = CodeGenerationUtils.dataSoruec(url, username, password);
		mpg.setDataSource(dsc);
		//配置  生成表的名称
		String[] tableName=tableNames.split(",");
		StrategyConfig sc = CodeGenerationUtils.strategyConfig(tableName, pc);
		mpg.setStrategy(sc);
		//
		InjectionConfig cfg = CodeGenerationUtils.injectionConfig(pc);
		mpg.setCfg(cfg);
		// 配置模板
		TemplateConfig templateConfig = new TemplateConfig();
		templateConfig.setXml(null);
		templateConfig.setController("/templates/vm/templateController.java.vm");
		mpg.setTemplate(templateConfig);
		mpg.execute();
		String filePath = ControlUtils.resourcePath + "/generationCode/" + stamp;
		System.out.println(filePath);
		return Result.success(filePath);
	}

	public static void main(String[] args) {
		String url="jdbc:mysql://42.192.231.8:3306/feifun";
		String username="root";
		String password="wkf@2023";
		String packages="com.fun.feifun";
		String module="user";
		String tableNames="sys_user,sys_role,sys_menu";
		Connection connection = null;
		List<String> tableList= new ArrayList<>();
		AutoGenerator mpg =new AutoGenerator();
		//配置生成的 包路径
		long stamp = System.currentTimeMillis();

		GlobalConfig gc = CodeGenerationUtils.globalConfig(stamp);
		mpg.setGlobalConfig(gc);
		//配置模块名
		PackageConfig pc = CodeGenerationUtils.packageConfig(packages,module);
		mpg.setPackageInfo(pc);
		//配置数据源
		DataSourceConfig dsc = CodeGenerationUtils.dataSoruec(url, username, password);
		mpg.setDataSource(dsc);
		//配置  生成表的名称
		String[] tableName=tableNames.split(",");
		StrategyConfig sc = CodeGenerationUtils.strategyConfig(tableName, pc);
		mpg.setStrategy(sc);
		//
		InjectionConfig cfg = CodeGenerationUtils.injectionConfig(pc);
		mpg.setCfg(cfg);
		// 配置模板
		TemplateConfig templateConfig = new TemplateConfig();
		templateConfig.setXml(null);
		templateConfig.setController("/templates/vm/templateController.java.vm");
		mpg.setTemplate(templateConfig);
		mpg.execute();
		String filePath = ControlUtils.resourcePath + "/generationCode/" + stamp;
		System.out.println(filePath);
	}

	/**
	 * 根据产品族、产品类型下载照片包
	 */
	@GetMapping("/downloadw")
	@ApiOperation(value = "下载代码压缩包")
	public void downloadw(HttpServletRequest request, HttpServletResponse response, String fileName)
			throws Exception {
		String filePath = ControlUtils.resourcePath + "/generationCode/" + fileName;
		response.setHeader("Content-Disposition", "attachment;fileName="+fileName+".zip");
		ZipUtils.toZip(filePath,response.getOutputStream(),true);
	}

}

